#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2024, Lime Technology
 * Copyright 2012-2024, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
extract(parse_plugin_cfg('dynamix',true));

// add translations
$_SERVER['REQUEST_URI'] = 'main';
$login_locale = _var($display,'locale');
require_once "$docroot/webGui/include/Translations.php";

$month = [' Jan '=>'-01-',' Feb '=>'-02-',' Mar '=>'-03-',' Apr '=>'-04-',' May '=>'-05-',' Jun '=>'-06-',' Jul '=>'-07-',' Aug '=>'-08-',' Sep '=>'-09-',' Oct '=>'-10-',' Nov '=>'-11-',' Dec '=>'-12-'];
$log   = "/boot/config/parity-checks.log";
$list  = [];

function this_plus($val, $word, $last) {
  return $val>0 ? (($val||$last)?($val.' '.$word.($last?'':', ')):'') : '';
}
function this_duration($time) {
  if (!$time) return 'Unavailable';
  $days = floor($time/86400);
  $hmss = $time-$days*86400;
  $hour = floor($hmss/3600);
  $mins = floor($hmss/60)%60;
  $secs = $hmss%60;
  return this_plus($days,_('day'),($hour|$mins|$secs)==0).this_plus($hour,_('hr'),($mins|$secs)==0).this_plus($mins,_('min'),$secs==0).this_plus($secs,_('sec'),true);
}
?>
<table style='margin-top:10px;background-color:inherit'>
<tr style='font-weight:bold'><td><?=_('Action')?></td><td><?=_('Date')?></td><td><?=_('Size')?></td><td><?=_('Duration')?></td><td><?=_('Speed')?></td><td><?=_('Status')?></td><td><?=_('Errors')?></td></tr>
<?
if (file_exists($log)) {
  $handle = fopen($log, 'r');
  while (($row = fgets($handle))!==false) {
    [$date,$duration,$speed,$status,$error,$action,$size] = my_explode('|',$row,7);
    $action = preg_split('/\s+/',$action);
    switch ($action[0]) {
      case 'recon': $action = in_array($action[1],['P','Q']) ? _('Parity-Sync') : _('Data-Rebuild'); break;
      case 'check': $action = count($action)>1 ? _('Parity-Check') : _('Read-Check'); break;
      case 'clear': $action = _('Disk-Clear'); break;
      default     : $action = '-'; break;
    }
    $date = str_replace(' ',', ',strtr(str_replace('  ',' 0',$date),$month));
    $date .= ' ('._(date('l',strtotime($date)),0).')';
    $size = $size ? my_scale($size*1024,$unit,-1)." $unit" : '-';
    $duration = this_duration($duration);
    // handle both old and new speed notation
    $speed = $speed ? (is_numeric($speed) ? my_scale($speed,$unit,1)." $unit/s" : $speed) : _('Unavailable');
    $status = $status==0 ? _('OK') : ($status==-4 ? _('Canceled') : $status);
    array_unshift($list, "<tr><td>$action</td><td>$date</td><td>$size</td><td>$duration</td><td>$speed</td><td>$status</td><td>$error</td></tr>");
  }
  fclose($handle);
}
echo $list ? implode($list) : "<tr><td colspan='7' style='text-align:center;padding-top:12px'>"._('No parity check history present')."!</td></tr>";
?>
</table>
